/**这里写一些说明*/
package com.pjedu.nbmalluser.service.imp;

import java.util.HashMap;

import com.pjedu.nbmallcommon.common.ServiceResultEnum;
import com.pjedu.nbmallcommon.utils.MD5Util;
import com.pjedu.nbmallcommon.utils.PageResult;
import com.pjedu.nbmalluser.entity.MallUser;
import com.pjedu.nbmalluser.mapper.MallUserMapper;
import com.pjedu.nbmalluser.service.MallUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MallUserServiceImpl implements MallUserService {

    @Resource
    private MallUserMapper mallUserMapper;

    @Override
    public MallUser getUserDetailById(Long loginUserId) {
        return mallUserMapper.selectByPrimaryKey(loginUserId);
    }

    @Override
    public PageResult getMallUsersPage(HashMap pageUtil) {
        List<MallUser> mallUsers = mallUserMapper.findMallUserList(pageUtil);
        int total = mallUserMapper.getTotalMallUsers(pageUtil);
        PageResult pageResult = new PageResult(mallUsers, total, Integer.valueOf(pageUtil.get("limit").toString()), Integer.valueOf(pageUtil.get("page").toString()));
        return pageResult;
    }

    @Override
    public String register(String loginName, String password) {
        if (mallUserMapper.selectByLoginName(loginName) != null) {
            return ServiceResultEnum.SAME_LOGIN_NAME_EXIST.getResult();
        }
        MallUser registerUser = new MallUser();
        registerUser.setLoginName(loginName);
        registerUser.setNickName(loginName);
        String passwordMD5 = MD5Util.MD5Encode(password, "UTF-8");
        registerUser.setPasswordMd5(passwordMD5);
        if (mallUserMapper.insertSelective(registerUser) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public MallUser login(String loginName, String passwordMD5) throws Exception {
        MallUser user = mallUserMapper.selectByLoginNameAndPasswd(loginName, passwordMD5);
        if(user == null) {
            return null;
        }
        if(user.getLockedFlag() == 1) {
            throw new Exception(ServiceResultEnum.LOGIN_USER_LOCKED.getResult());
        }
        return user;
    }

    @Override
    public void updateUserInfo(MallUser mallUser) {
        mallUserMapper.updateByPrimaryKeySelective(mallUser);
    }

    @Override
    public Boolean lockUsers(Integer[] ids, int lockStatus) {
        if (ids.length < 1) {
            return false;
        }
        return mallUserMapper.lockUserBatch(ids, lockStatus) > 0;
    }
}
